
'use strict';

const path = require('path');
const fs = require('fs');
const exe = require('child_process');
const { Controller } = require('ee-core');
const { app: electronApp, shell } = require('electron');
const dayjs = require('dayjs');
const Ps = require('ee-core/ps');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const Conf = require('ee-core/config');
const Addon = require('ee-core/addon');
const EE = require('ee-core/ee');
const iconv = require('iconv-lite');
let childProcess = null;
class FrameworkController extends Controller{
    constructor(ctx) {
        super(ctx);
    }
    /**
     * 调用其它程序（exe、bash等可执行程序）
     */
    openSoftware(softName) {
        if (!softName) {
            return false;
        }
        // 若已运行程序则杀死进程
        try {
            if (childProcess) {
                exe.exec("taskkill /f /im frp-client.exe")
                childProcess.kill("SIGINT")
                childProcess = null
                return "0";
            }
        }catch (e) {
            console.log(e)
        }
        let softwarePath = path.join(Ps.getExtraResourcesDir(), softName);
        Log.info(softwarePath);
        // 命令行字符串 并 执行
        // var childProcess = exe.exec(softwarePath);
        // childProcess.stdout.on('data', function (data) {
        //     Log.info(data);
        // });
        childProcess = exe.exec(softwarePath);
        childProcess.stdout.on('data', function (data) {
            console.log(data)
        });

// 打印错误的后台可执行程序输出
        childProcess.stderr.on('data', function (data) {
            console.log(data)
        });

// 退出之后的输出
        childProcess.on('close', function (code) {
            console.log('out code：' + code);
        })
// 监听子进程退出事件
        childProcess.on('exit', (code, signal) => {
            console.log(`子进程已退出，退出码： ${code}，信号： ${signal}`);
        });
        return true;
    }
}


FrameworkController.toString = () => '[class FrameworkController]';
module.exports = FrameworkController;